//
//  HSPShopperViewController.m
//  Icar
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "HSPShopperViewController.h"
#import "BaseHeader.h"
#import "HSPriceCollectionViewCell.h"
#import "AFNBlock.h"
#import "HSPpriceMoneyViewController.h"
#import "HSPShopWebViewController.h"
@interface HSPShopperViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UITableView *tableViewShopper;
@property (nonatomic,strong)NSArray *priceArray;
@property (nonatomic,strong)NSMutableArray *shopperArray;
@property (nonatomic,strong)UICollectionView *priceRangeView;
@property (nonatomic,assign)BOOL isUp;
@property (nonatomic,assign)NSInteger page;
@end

@implementation HSPShopperViewController
- (void)viewWillAppear:(BOOL)animated
{
    [_tableViewShopper reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor: [UIColor whiteColor]];
    self.shopperArray = [NSMutableArray array];
    self.priceArray = [[NSArray alloc]initWithObjects:@"≤10",@"10-15",@"15-20",@"20-30",@"30-50",@"≥50", nil];
    [self shopper];
    [self collection];
    [self AlbumaHeader];
     [self AlbumaFooter];
}
- (void)shopper
{
    _tableViewShopper = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ADJUSTHEIGHT * 520) style:UITableViewStylePlain];
    _tableViewShopper.delegate =self;
    _tableViewShopper.dataSource = self;
    [self.view addSubview:_tableViewShopper];
}
- (void)collection
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(60 * ADJUSTWIDTH, 60 * ADJUSTHEIGHT);
    flowLayout.minimumInteritemSpacing = 25 * ADJUSTHEIGHT;
    flowLayout.minimumLineSpacing = 25 * ADJUSTWIDTH;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10 * ADJUSTHEIGHT, 0, 25 * ADJUSTHEIGHT);
    
    self.priceRangeView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 30 * ADJUSTHEIGHT, ADJUSTWIDTH * 355, 120 * ADJUSTHEIGHT) collectionViewLayout:flowLayout];
    self.priceRangeView.backgroundColor = [UIColor grayColor];
    self.priceRangeView.pagingEnabled = YES;
    self.priceRangeView.delegate = self;
    self.priceRangeView.dataSource = self;
    self.tableViewShopper.tableHeaderView = self.priceRangeView;

    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5 * ADJUSTWIDTH, 5 * ADJUSTHEIGHT, SCREEN_WIDTH, 30 * ADJUSTHEIGHT)];
    [label setText:@"价格选择区间(单位:万元)"];
    [label setTextColor:[UIColor whiteColor]];
    [label setFont:[UIFont systemFontOfSize:15.]];
    [self.priceRangeView addSubview:label];
    [self.priceRangeView registerClass:[HSPriceCollectionViewCell class] forCellWithReuseIdentifier:@"shoppercell"];
}
- (void)popBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HSPriceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"shoppercell" forIndexPath:indexPath];
    [cell.labelPrice setText:[self.priceArray objectAtIndex:indexPath.row]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HSPpriceMoneyViewController *price = [[HSPpriceMoneyViewController alloc]init];
    price.titleString = [self.priceArray objectAtIndex:indexPath.row];
    switch (indexPath.row) {
        case 0:
            price.priceUrl = PRICE10;
            break;
        case 1:
            price.priceUrl = PRICE15;
            break;
        case 2:
            price.priceUrl = PRICE20;
            break;
        case 3:
            price.priceUrl = PRICE30;
            break;
        case 4:
            price.priceUrl = PRICE40;
            break;
        case 5:
            price.priceUrl = PRICE50;
            break;
        default:
            break;
    }
    [self.navigationController pushViewController:price animated:YES];
}
#pragma 上拉刷新
- (void)AlbumaHeader
{
    __block HSPShopperViewController *vc = self;
    [_tableViewShopper addLegendHeaderWithRefreshingBlock:^{
        vc.page = 1;
        [vc APPadndatanumber :1];
        _isUp = YES;
    }];
    [_tableViewShopper.header beginRefreshing];
}
#pragma 下拉加载
- (void)AlbumaFooter
{
    __block HSPShopperViewController *vcc = self;
    [_tableViewShopper addLegendFooterWithRefreshingBlock:^{
        vcc.page = vcc.page + 1;
        [vcc APPadndatanumber:vcc.page];
        _isUp = NO;
    }];
}
- (void)APPadndatanumber:(NSInteger)number
{
    NSString *documentString = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *pathString = [documentString stringByAppendingString:@"Shop.plist"];
    if ([Reachability isConnectedInternet] == YES) {
        [AFNBlock getDataWithString:[NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/cms/channels/3?pageNo=%ld&pageSize=20&v=4.0.0",_page] block:^(id back) {
        
            if (_isUp == YES) {
                [self.shopperArray removeAllObjects];
            }
            for (NSDictionary *dic in [back objectForKey:@"data"]) {
                HSPShopperModel *model = [HSPShopperModel modelWithDictionary:dic];
                [self.shopperArray addObject:model];
            }
            [self.tableViewShopper reloadData];
            [self.tableViewShopper.header endRefreshing];
            [self.tableViewShopper.footer endRefreshing];
            [NSKeyedArchiver archiveRootObject:_shopperArray toFile:pathString];

        }];
    }
    if (ISCONENNCTED == NO) {
        if (_isUp == YES) {
            [_shopperArray removeAllObjects];
        }
        //读取
        self.shopperArray = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithFile:pathString]];
        [_tableViewShopper reloadData];
        [_tableViewShopper.header endRefreshing];
        [_tableViewShopper.footer endRefreshing];
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.shopperArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85 * ADJUSTHEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSPFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shopper"];
    if (cell == nil) {
        cell = [[HSPFirstCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"shopper"];
    }
    HSPShopperModel *data = [self.shopperArray objectAtIndex:indexPath.row];
    self.tableViewShopper.scrollsToTop = YES;
    [cell getShopImage:data];
    cell.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor;
    cell.titleLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSPShopWebViewController *pushS = [[HSPShopWebViewController alloc]init];
    pushS.model = [self.shopperArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:pushS animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
